//
//  MineViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/26.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "MineViewController.h"
#import "JHMacro.h"
#import "LoginViewController.h"
#import "CustomCell1.h"
#import "CustomCell2.h"
#import "CustomCell3.h"
#import "JHAccountTool.h"
#import "UIImageView+WebCache.h"
#import "SettingTableViewController.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *MineTableView;
@property(nonatomic,strong)UIView *headerView;// 头视图
@property(nonatomic,strong)UIImageView *bigimgview;// 头视图背景色
@property(nonatomic,assign)NSInteger isLogin;
@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _isLogin = [user integerForKey:@"isLogin"];
    if (_isLogin == 0) {
        self.smallimgview.image = [UIImage imageNamed:@"7"];
        self.nameLabel.text = @"登录";
    } else if (_isLogin == 1) {
        [self refreshLabel];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 监控
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelText:) name:@"ChangeLabelTextNotification" object:nil];
    // tableView初始化
    self.MineTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 49) style:(UITableViewStylePlain)];
    // 注册Cell
    [self.MineTableView registerNib:[UINib nibWithNibName:@"CustomCell1" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self.MineTableView registerNib:[UINib nibWithNibName:@"CustomCell3" bundle:nil] forCellReuseIdentifier:@"cell3"];
    // 创建头视图
    [self createHeaderView];
    // 指定代理
    self.MineTableView.delegate = self;
    self.MineTableView.dataSource = self;
    // 添加视图到view上
    [self.view addSubview:self.MineTableView];
    // 设置手势识别器
    UITapGestureRecognizer *Click;
    Click = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(PictureClick:)];
    [self.smallimgview addGestureRecognizer:Click];
    Click.numberOfTapsRequired = 1;
}
// 监控方法
- (void)changeLabelText:(NSNotification *)notification
{
    id text = notification.object;
    UILabel *label = self.nameLabel;
    label.text = text;
}
// 登录后传值赋值
- (void)refreshLabel {
    [self.smallimgview sd_setImageWithURL:[NSURL URLWithString:self.imageStr]];
    self.nameLabel.text = self.nameStr;
    [self.MineTableView reloadData];
}
// 创建头视图
- (void)createHeaderView {
    // 创建头视图
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    // 初始化背景色图片
    self.bigimgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    // 背景色颜色
    self.bigimgview.backgroundColor = [UIColor colorWithRed:1.000 green:0.827 blue:0.000 alpha:1.000];
    // 添加到头视图上
    [self.headerView addSubview:self.bigimgview];
    // 初始化头像图片
    self.smallimgview = [[UIImageView alloc]initWithFrame:CGRectMake(self.headerView.frame.size.width / 2 - 45, self.headerView.frame.size.height - 155, 90, 90)];
    self.smallimgview.contentMode = UIViewContentModeScaleToFill;
    self.smallimgview.image = [UIImage imageNamed:@"7"];
    // 设置可被点击
    self.smallimgview.userInteractionEnabled = YES;
    // 添加到头视图上
    [self.headerView addSubview:self.smallimgview];
    // 头像图片背景色
    self.smallimgview.backgroundColor = [UIColor grayColor];
    // 设置圆角
    CALayer *layer = self.smallimgview.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:45];
    // 初始化昵称Label
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height - 55, SCREEN_WIDTH, 30)];
    // 假数据
    self.nameLabel.text = @"登录";
    // 文字居中显示
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    // 添加到头视图上
    [self.headerView addSubview:self.nameLabel];
    // 设置成tableView头视图
    self.MineTableView.tableHeaderView = self.headerView;
}
// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
// 每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 3;
    } else {
        return 1;
    }
}
// 滑动时让头视图拉伸放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset_Y = scrollView.contentOffset.y;
    if (offset_Y < 0) {
        CGFloat add_height = -(offset_Y + 0);
        CGFloat scale = (add_height + 200) / 200.0f;
        self.bigimgview.frame = CGRectMake(-(self.headerView.bounds.size.width * scale - self.headerView.bounds.size.width) / 2.0f, - add_height, self.headerView.bounds.size.width * scale, 250 + add_height);
    }
}
// 设置tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 || (indexPath.section == 1 && indexPath.row == 2)) {
        CustomCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            cell.namelabel.text = @"我的消息";
            cell.imgview.image = [UIImage imageNamed:@"003"];
        } else if (indexPath.section == 1 && indexPath.row == 0) {
            cell.namelabel.text = @"我的关注";
            cell.imgview.image = [UIImage imageNamed:@"005"];
        } else if (indexPath.section == 1 && indexPath.row == 2) {
            cell.namelabel.text = @"我的收藏";
            cell.imgview.image = [UIImage imageNamed:@"006"];
        } else {
            cell.namelabel.text = @"设置";
            cell.imgview.image = [UIImage imageNamed:@"008"];
        }
        return cell;
    } else {
        CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        cell.imgview.image = [UIImage imageNamed:@"007"];
        return cell;
    }
}
#pragma mark 未解决:设置界面
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 69;
}
// 分区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 7;
}
// 轻拍手势方法
-(void)PictureClick: (UITapGestureRecognizer *)sender{
    self.smallimgview = (UIImageView *)sender.view;
    if ([_nameLabel.text  isEqual: @"登录"]) {
        // 如果未登录，则模态弹出登录界面
        LoginViewController *LoginVC = [[LoginViewController alloc]init];
        LoginVC.lastVC = self;
        // 给登录界面添加导航栏
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:LoginVC];
        // 模态跳转
        [self presentViewController:nvc animated:YES completion:nil];
        
    } else {
        // 如果已登录，则弹出提示查看详情
    }
}
// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSString *userName = [JHAccountTool getUserName];
//    NSLog(@"%@",userName);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"isLogin"] == NO) {
        // 如果未登录，则模态弹出登录界面
        LoginViewController *LoginVC = [[LoginViewController alloc]init];
        // 给登录界面添加导航栏
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:LoginVC];
        // 模态跳转
        [self presentViewController:nvc animated:YES completion:nil];
    } else {
        // 如果已登录
        if (indexPath.section == 2 && indexPath.row == 0) {
            SettingTableViewController *vc = [[SettingTableViewController alloc]init];
            UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:vc];
            [self presentViewController:navc animated:YES completion:nil];
//            self.nameLabel.text = @"登录";
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
